//查询数据 渲染页面
say.artwork.index.query = function(pageNo){
	say.artwork.index.currentPage = pageNo;
	var url = "/say/artwork/rest/queryPage";
	var param = {
		classificationId:say.artwork.index.classificationId,
		pageSize:9,
		pageNo:pageNo
	}
	$.ajax({
		type: 'POST',
		url: url,
       	data: param,
       	dataType:'json',	
       	success: function (result) {	
	   		if(result && result.status){
				if(result.status==1 && result.data){
					$("#ARTWORK_NODE").empty();
					for (var i=0; i<result.data.length; i+=3){
						$("#ARTWORK_NODE").append(_.template($("#ROW_ITEM").html())({i: i}));
						
						$("#ARTWORK_ROW"+i).append(_.template($("#ARTWORK_ITEM").html())({info: result.data[i]}));
						if (i+1<result.data.length) $("#ARTWORK_ROW"+i).append(_.template($("#ARTWORK_ITEM").html())({info: result.data[i+1]}));
						if (i+2<result.data.length) $("#ARTWORK_ROW"+i).append(_.template($("#ARTWORK_ITEM").html())({info: result.data[i+2]}));
					}
					if (result.data.length>0){
						base.jqPaginator("pager",result.page,10,"say.artwork.index.query");
					}
				}else{
					$("#ARTWORK_NODE").html("没有数据");
				}
			}else{
				layer.msg("请求失败",{icon:0});
			}
       	},
       	error: function(result){
       		alert("未知错误");
       	}
	});
}

//新增作品
say.artwork.index.add = function(){
	var url = "/say/artwork/rest/save";
	var formData = base.serializeObject($("#addArtworkForm"));
	formData.classificationId = say.artwork.index.classificationId;
	$.ajax({
		type: 'POST',
		url: url,
		data: JSON.stringify(formData),
       	dataType: 'json',	
       	contentType: 'application/json',
       	success: function (result) {	
	   		if(result && result.status){
				if(result.status==1){
					layer.msg("新增作品成功",{icon:1});
					layer.close(say.artwork.index.layerIndex);
					say.artwork.index.query();
				}else{
					layer.msg(result.msg,{icon:0});
				}
			}
       	},
       	error: function(result){
       		layer.msg("未知错误",{icon:0});
       	}
	});
}

//新增作品
say.artwork.index.addArtwork = function(){
	say.artwork.index.layerIndex = layer.open({
        type: 1,
        title: "请填写作品信息",
        area: ["600px", "500px"],
        content: $("#ADD_ARTWORK").html(),
        success: function(index){
        	$("#artworkAdd").click(say.artwork.index.add);
        	$("#artworkCancel").click(function(){
        		layer.close(say.artwork.index.layerIndex);
        	});
        }
	});
}

//作品点击  跳转进详细
say.artwork.index.artworkInto = function(){
	var artworkId = $(this).attr("artworkId");
//	window.open("/say/commentary/index?id="+artworkId);
	
	var aHtml = '<a href="/say/commentary/index?id='+artworkId+'" target ="_blank" class="btn btn-default" role="button" style="display:none;">详情</a>';
	$(this).append(aHtml);
	$(aHtml)[0].click();
}

$(function(){
	say.artwork.index.query(1);
	
	//绑定事件
	$("#addArtwork").on("click",say.artwork.index.addArtwork);	//新增
	$("#ARTWORK_NODE").on("click",".artwork-into",say.artwork.index.artworkInto);
	
	
});
